package acheng1314.cn.a3dbuild2.view.activity.presenter;

import com.acheng.achengutils.gsonutil.GsonUtils;
import com.acheng.achengutils.mvp.presenter.BasePresenter;
import com.acheng.achengutils.utils.CipherUtils;
import com.acheng.achengutils.utils.StringUtils;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import acheng1314.cn.a3dbuild2.MyApplication;
import acheng1314.cn.a3dbuild2.bean.LoginBean;
import acheng1314.cn.a3dbuild2.hostApi.MyApi;
import acheng1314.cn.a3dbuild2.view.activity.viewcontroller.LoginActivityViewController;

/**
 * Created by pc859107393 on 2016/9/12 0012.
 */
public class LoginActivityPresenter extends BasePresenter<LoginActivityViewController> {
    /**
     * 在子类的构造函数中，设定参数为model，这时候可以presenter调用接口来实现对界面的操作。
     *
     * @param model
     */
    public LoginActivityPresenter(LoginActivityViewController model) {
        super(model);
    }

    @Override
    public void initData() {

    }

    public void doLogin(String name, String pwd) {
        //用户名和密码不能为空
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(pwd)) {
            model.showDailog("用户名或密码不能为空！");
            return;
        }

        //密码MD5加密
        pwd = CipherUtils.small32md5(pwd);
        HttpParams params = new HttpParams();
        params.put("username", name);
        params.put("userpwd", pwd);
        RxVolley.post(MyApi.LoginApi, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                //数据不为空再进行数据处理
                try {
                    if (null != t) {
                        MyApplication.getInstance().outLog("输出", t);
                        LoginBean bean = new GsonUtils().toBean(t, LoginBean.class);
                        if (null != bean) {
                            if (bean.getCode() == 1001) {
                                //请求成功
                                model.openHome(bean);
                            } else if (bean.getCode() == 1) {
                                model.showDailog("登录失败,帐户不存在");
                            } else if (bean.getCode() == 2) {
                                model.showDailog("登录失败,密码错误");
                            } else {
                                model.showDailog("登录失败,其他未知错误");
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    model.showDailog("登录失败,其他未知错误");
                }


            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                model.showDailog("登录失败,其他未知错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                model.disProgressD();
            }

            @Override
            public void onPreStart() {
                super.onPreStart();
                model.showProgressD();
            }
        });


    }
}
